﻿@model IEnumerable<Models.ItemList>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <environment include="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" media="all" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="~/layer/css/layui.css" asp-append-version="true" media="all" />
        <link rel="stylesheet" href="~/layer/css/admin.css" asp-append-version="true" media="all" />
    </environment>
</head>
<body id="iosiframe">
    <div class="layui-fluid">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header">推荐机构 </div>

                    <div class="layui-card-body">
                        <div style="padding-bottom: 10px;">
                            <a class="layui-btn layuiadmin-btn-admin" data-type="add" href="@Url.Action("recommend","account", new { userType = ViewData["userTypes"]})">添加</a>
                        </div>


                        <table class="layui-hide" id="table-page" lay-filter="table"></table>

                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="~/layer/layui.js"></script>
    <script>
        layui.config({
            base: '../../layer/' //静态资源所在路径
        }).extend({
            index: 'index' //主入口模块
            }).use(['form', 'index', 'table'], function () {
                var $ = layui.$
                    , form = layui.form
                    , table = layui.table;

                var ua = navigator.userAgent.toLowerCase();
                var screenwidth = window.screen.width;
                if (!/iphone|ipad|ipod/.test(ua)) {
                    $("#iosiframe").attr("scrolling", "auto");
                } else {
                    $('#iosiframe').width(screenwidth + 'px');
                }

                var url = '@Url.Action("userlist", "api", new { userId = ViewData["Id"] })';

                $.ajax({
                    url: '@Url.Action("getuserlistcolumns", "api",new { userType = (int)Code.UserTypes.普通用户 })',
                    dataType: 'json',
                    contentType: 'application/json',
                    type: 'get'
                }).fail(function (jqXHR, textStatus, errorThrown) {
                    window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                }).done(function (res) {
                    res.splice(0, 0, { type: 'checkbox' });
                    res.push({ width: 130, align: 'center',  toolbar: '#buttonTpl' });

                    var tableIns = table.render({
                        elem: '#table-page'
                        , cellMinWidth: 80
                        , limit: 30
                        , url: url
                        , toolbar: true
                        , defaultToolbar: ['filter', 'print', 'exports']
                        , cols: [res]
                        , parseData: function (res) {
                            return {
                                code: 0,
                                msg: "",
                                count: res.count,
                                data: JSON.parse(res.data)
                            }
                        }
                        , page: true
                    });

                    form.on('submit(LAY-user-back-search)', function (data) {
                        var field = data.field;

                        //执行重载
                        tableIns.reload({
                            where: field
                        });
                    });

                    var del = function (ids, table) {
                        console.log(ids);
                        $.ajax({
                            url: '@Url.Action("delusers", "api")'
                            , dataType: 'json'
                            , data: { ids: ids }
                            , contentType: 'application/json'
                            , type: 'get'
                        }).fail(function (jqXHR, textStatus, errorThrown) {
                            window.parent.tools.alert(errorThrown, { icon: 2, title: textStatus });
                        }).done(function (res) {
                            window.parent.tools.msg(res.message, { offset: '15px', icon: (res.code == 0 ? 1 : 2), time: 1000 });

                            if (res.code == 0) {
                                table.reload();
                            }
                        });
                    }


                    var active = {
                        batchdel: function () {
                            var checkStatus = table.checkStatus('table-page')
                                , checkData = checkStatus.data;

                            if (checkData.length === 0) {
                                return layer.msg('请选择数据');
                            }

                            window.parent.tools.confirm('确定删除吗？', function () {
                                var ids = [];

                                checkData.forEach(function (item) { ids.push(item.Id) });

                                del(ids.join(','), tableIns);
                            });
                        }, add: function (obj) {
                            console.log(obj);
                        }
                    }

                    $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                    table.on('tool(table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                        var data = obj.data; //获得当前行数据
                        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                        var tr = obj.tr; //获得当前行 tr 的DOM对象

                        if (layEvent === 'detail') { //查看
                            //do somehing
                        } else if (layEvent === 'del') { //删除
                            window.parent.tools.confirm('确定删除吗？', function (index) {
                                del(obj.data.Id, tableIns);
                            });
                        } else if (layEvent === 'edit') { //编辑
                            location.href = '@Url.Action("recommend", "account")/' + data.Id;
                        }
                    });
                });

            });


    </script>
</body>
</html>
